#include "scalewheeleater.h"
#include "wheeleater.h"

ScaleWheelEater::ScaleWheelEater(QWidget *parent, QWidget *target, ScaleWheelEaterFunc func_before, ScaleWheelEaterFunc func_after, std::function<void()> finally)
    : QObject{}
    , m_parent(parent)
    , m_target(target)
    , m_func_before(func_before)
    , m_func_after(func_after)
    , m_finally(finally)
    , m_ratio(100)
{
    if (target == nullptr)
    {
        m_target = target;
    }

    new WheelEater([this](QWheelEvent * event)
    {
        static QPoint degress;
        degress = event->angleDelta();

        int step = 0;
        if (degress.isNull() == false)
        {
            step = degress.y();
        }

        int currentWidth = m_target->width();      // 获取当前图像的宽
        int currentHeight = m_target->height();    // 获取当前图像的高

        double ratio = (m_ratio / 100.0);
        currentWidth += (step * ratio);  // 对当前图像的高累加
        currentHeight += (step * ratio); // 对当前图像的宽累加

        auto oldsize = m_target->size();
        auto newsize = QSize(currentWidth, currentHeight);

        if (m_func_before != nullptr)
        {
            m_func_before(event, oldsize, newsize);
        }
        m_target->resize(currentWidth, currentHeight);
        if (m_func_after != nullptr)
        {
            m_func_after(event, oldsize, newsize);
        }
        if (m_finally != nullptr)
        {
            m_finally();
        }
        return false;
    }, m_parent);
}

void ScaleWheelEater::setRatio(int ratio)
{
    m_ratio = ratio;
}
